package com.thinkfit.jy.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.lib.bean.SportInfoBean;
import com.common.lib.bean.SportRecordBean;
import com.common.lib.bean.VideoBean;
import com.common.lib.fragment.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.thinkfit.jy.R;
import com.thinkfit.jy.adapter.SportRecordAdapter;
import com.thinkfit.jy.contract.SportRecordContract;
import com.thinkfit.jy.presenter.SportRecordPresenter;

import java.util.ArrayList;

/**
 * 运动记录界面
 */
public class SportRecordDetailFragment extends BaseFragment<SportRecordContract.Presenter> implements SportRecordContract.View, OnRefreshLoadMoreListener {

    private UserProfileFragment mUserProfileFragment;
    private SportRecordAdapter mAdapter;

    private int mPage;

    @NonNull
    @Override
    protected SportRecordContract.Presenter onCreatePresenter() {
        return new SportRecordPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sport_record;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        getAdapter().onAttachedToRecyclerView(recyclerView);
        recyclerView.setAdapter(getAdapter());
        SmartRefreshLayout layout = view.findViewById(R.id.smartRefreshLayout);
        layout.setOnRefreshLoadMoreListener(this);
        layout.setEnableLoadMore(false);
        layout.autoRefresh();
    }

    private SportRecordAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new SportRecordAdapter(getActivity());
            mAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                }
            });
        }
        return mAdapter;
    }

    public SportRecordDetailFragment setParentFragment(UserProfileFragment fragment) {
        mUserProfileFragment = fragment;
        return this;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void getSportInfoSuccess(SportInfoBean bean) {
        if (getView() == null) {
            return;
        }
        setText(R.id.tvTotalTime, String.valueOf(bean.getAlltime() / 60));
        setText(R.id.tvDistance, bean.getDistance());
        setText(R.id.tvKcal, bean.getKcal());
    }

    @Override
    public void getSportRecordSuccess(int page, ArrayList<SportRecordBean> list) {
        if (getView() == null) {
            return;
        }
        mPage = page;
        if (page == 1) {
            getAdapter().setNewInstance(list);
        } else {
            getAdapter().addData(list);
        }
        finishLoad();
    }

    @Override
    public void getSportRecordFailed() {
        if (getView() == null) {
            return;
        }
        finishLoad();
    }

    @Override
    public void getVideoCourseFailed() {

    }

    @Override
    public void getVideoCourseSuccess(int page, ArrayList<VideoBean> list) {

    }

    private void finishLoad() {
        SmartRefreshLayout layout = getView().findViewById(R.id.smartRefreshLayout);
        layout.finishLoadMore();
        layout.finishRefresh();
        layout.setEnableLoadMore(getAdapter().getItemCount() != 0
                && getAdapter().getItemCount() % 9 == 0);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        getPresenter().getSportRecord(mPage + 1);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        getPresenter().getSportRecord(1);
        getPresenter().getSportInfo();
    }
}
